﻿using System;
using System.Collections.Generic;

// 抽象基类
abstract class Shape
{
    public abstract double GetArea();  // 计算面积
    public abstract bool IsValid();    // 判断是否合法
}

// 长方形类
class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    public override double GetArea()
    {
        return Width * Height;
    }

    public override bool IsValid()
    {
        return Width > 0 && Height > 0;
    }
}

// 正方形类
class Square : Rectangle
{
    public Square(double side) : base(side, side) { }
}

// 三角形类
class Triangle : Shape
{
    public double A { get; set; }
    public double B { get; set; }
    public double C { get; set; }

    public Triangle(double a, double b, double c)
    {
        A = a;
        B = b;
        C = c;
    }

    public override double GetArea()
    {
        double s = (A + B + C) / 2;
        return Math.Sqrt(s * (s - A) * (s - B) * (s - C));
    }

    public override bool IsValid()
    {
        return A + B > C && A + C > B && B + C > A && A > 0 && B > 0 && C > 0;
    }
}

// 简单工厂模式：随机创建形状
class ShapeFactory
{
    private static Random random = new Random();

    public static Shape CreateRandomShape()
    {
        int shapeType = random.Next(3); // 0: Rectangle, 1: Square, 2: Triangle
        switch (shapeType)
        {
            case 0: // 随机长方形
                double width = random.Next(1, 10);
                double height = random.Next(1, 10);
                return new Rectangle(width, height);

            case 1: // 随机正方形
                double side = random.Next(1, 10);
                return new Square(side);

            case 2: // 随机三角形（保证合法）
                while (true)
                {
                    double a = random.Next(1, 10);
                    double b = random.Next(1, 10);
                    double c = random.Next(1, 10);
                    Triangle triangle = new Triangle(a, b, c);
                    if (triangle.IsValid()) return triangle;
                }

            default:
                return null;
        }
    }
}

class Program
{
    static void Main()
    {
        List<Shape> shapes = new List<Shape>();
        double totalArea = 0;

        for (int i = 0; i < 10; i++)
        {
            Shape shape = ShapeFactory.CreateRandomShape();
            shapes.Add(shape);
            Console.WriteLine($"形状 {i + 1}: 类型 = {shape.GetType().Name}, 面积 = {shape.GetArea():F2}");
            totalArea += shape.GetArea();
        }

        Console.WriteLine($"\n所有形状的总面积: {totalArea:F2}");
    }
}